package cn.zdsoft.configuration.core.web;

import cn.zdsoft.configuration.core.Constants;
import cn.zdsoft.configuration.core.ConfigInitializer;
import cn.zdsoft.configuration.core.api.CallbackAble;
import cn.zdsoft.configuration.core.listener.ZKListener;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.Set;

/**
 * Created by wangdongdong on 2018/5/14.
 */
public class WebConfigInitializer extends ConfigInitializer implements ServletContainerInitializer {

    private ZKListener listener;

    private CallbackAble callbackAble;

    @Override
    public void stop() {

    }

    @Override
    public boolean isStarted() {
        return false;
    }

    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        WebConfigContext configContext = new DefaultWebConfigContext();
        configContext.setServletContext(servletContext);
        // 监听器
        listener = new ZKListener();
        // 回调
        callbackAble = new SpringCallbackAble();
        this.start(configContext);
        servletContext.setAttribute(Constants.CONFIG_CONTEXT, configContext);
    }

    @Override
    public ZKListener getListener() {
        return listener;
    }

    @Override
    public CallbackAble getCallbackAble() {
        return callbackAble;
    }
}
